package com.atguigu.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.common.to.SkuReductionTO;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.coupon.dao.SkuFullReductionDao;
import com.atguigu.coupon.entity.MemberPriceEntity;
import com.atguigu.coupon.entity.SkuFullReductionEntity;
import com.atguigu.coupon.entity.SkuLadderEntity;
import com.atguigu.coupon.service.MemberPriceService;
import com.atguigu.coupon.service.SkuFullReductionService;
import com.atguigu.coupon.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;

    @Resource
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        if (skuReductionTO.getFullCount() > 0) {
            //1.保存满减打折,会员价
            SkuLadderEntity skuLadderEntity = SkuLadderEntity.builder().skuId(skuReductionTO.getSkuId()).fullCount(skuReductionTO.getFullCount())
                    .discount(skuReductionTO.getDiscount()).addOther(skuReductionTO.getCountStatus()).build();
            skuLadderService.save(skuLadderEntity);
        }


        //2.满减
        if (skuReductionTO.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
            SkuFullReductionEntity skuFullReductionEntity = BeanUtil.copyProperties(skuReductionTO, SkuFullReductionEntity.class);
            save(skuFullReductionEntity);
        }

        List<MemberPriceEntity> memberPriceEntities = skuReductionTO.getMemberPrice().stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = BeanUtil.copyProperties(memberPrice, MemberPriceEntity.class);
            memberPriceEntity.setSkuId(skuReductionTO.getSkuId()).setMemberLevelId(memberPrice.getId())
                    .setMemberLevelName(memberPrice.getName()).setMemberPrice(memberPrice.getPrice()).setAddOther(1);
            return memberPriceEntity;
        }).filter(memberPriceEntity -> memberPriceEntity.getMemberPrice().compareTo(BigDecimal.ZERO) >0 ).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}